package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.utils.BaseContext;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartController shoppingCartController;

    /**
     * 订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 查询历史订单  前台
     * 1.从session中获取用户Id
     * 2.根据用户ID查询订单
     * 3.根据订单表Id到订单明细查询
     * 4.分页查询调用方法查询
     * @param page
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page, Integer pageSize){
        //1.从session中获取用户Id
        Long currentId = BaseContext.getCurrentId();

        //2.根据用户ID查询订单  分页查询
        LambdaQueryWrapper<Orders> orderLqw = new LambdaQueryWrapper<>();
        orderLqw.eq(currentId != null, Orders::getUserId, currentId);
        orderLqw.orderByAsc(Orders::getOrderTime);

        Page<OrdersDto> ordersDtoPage = new Page<>();
        //分页构造器  订单
        Page<Orders> ordersPage = new Page<>(page,pageSize);

        //3.调用方法查询并返回
        orderService.page(ordersPage, orderLqw);


        //获取菜单明细
        List<OrdersDto> ordersDtos = ordersPage.getRecords().stream().map((ordersItem) ->{
            //将orders 复制到ordersDto里面
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(ordersPage, ordersDto,"records");

            //获取订单id 到订单明细表查询
            LambdaQueryWrapper<OrderDetail> orderDetailLqw = new LambdaQueryWrapper<>();
            orderDetailLqw.eq(ordersItem.getId() != null, OrderDetail::getOrderId, ordersItem.getId());
            List<OrderDetail> detailList = orderDetailService.list(orderDetailLqw);
            ordersDto.setOrderDetails(detailList);

            BeanUtils.copyProperties(ordersItem,ordersDto);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtos);

        return R.success(ordersDtoPage);
    }


    /**
     * 订单明细 后台
     *
     * 1.根据传回来的条件查询
     * 2.分页查询
     * @param page
     * @param pageSize
     * @param
     * @param
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, Long number, String beginTime, String endTime){

        //创建分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);

        //1.根据传回来的条件查询
        LambdaQueryWrapper<Orders> ordersLqw = new LambdaQueryWrapper<>();
        ordersLqw.eq(number != null, Orders::getNumber, number);
        ordersLqw.between(beginTime !=null, Orders::getCheckoutTime, beginTime, endTime);
        orderService.page(ordersPage, ordersLqw);
        return R.success(ordersPage);
    }


    /**
     * 修改状态
     * 根据传回来的ID 修改
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("修改成功");
    }

    /**
     * 再来一单
     * 1.根据传回来的订单Id 查询订单详细表
     * 2.将订单详细表再次添加到购物车
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        //1.根据传回来的订单Id 查询订单详细表

        LambdaQueryWrapper<OrderDetail> orderDeteilLqw = new LambdaQueryWrapper<>();
        orderDeteilLqw.eq(orders.getId() != null, OrderDetail::getOrderId, orders.getId());

        //调用方法查询 返回菜品集合
        List<OrderDetail> orderlist = orderDetailService.list(orderDeteilLqw);

        //2.将订单详细表再次添加到购物车  创建购物车对象
        List<ShoppingCart> shopList =orderlist.stream().map((orderItem) ->{

            //创建购物车对象
            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.setName(BaseContext.getCurrentId().toString());        //设置名称
            shoppingCart.setImage(orderItem.getImage());               //设置图片
            shoppingCart.setNumber(orderItem.getNumber());             //设置数量
            shoppingCart.setDishId(orderItem.getDishId());             //设置菜品ID
            shoppingCart.setDishFlavor(orderItem.getDishFlavor());     //设置菜品口味
            shoppingCart.setCreateTime(LocalDateTime.now());           //设置创建时间
            shoppingCart.setAmount(orderItem.getAmount());             //设置金额
            shoppingCart.setSetmealId(orderItem.getSetmealId());       //设置套餐 Id

//            shoppingCart.setUserId(BaseContext.getCurrentId());        //设置主键Id  用户

            //给集合返回
            return shoppingCart;
        }).collect(Collectors.toList());

        for (ShoppingCart shoppingCart : shopList) {
            shoppingCartController.add(shoppingCart);
        }

        return R.success("添加成功");
    }

}